package com.scorpio.web.controller.agentuser;

import com.scorpio.agentuser.domain.AgentUserEntity;
import com.scorpio.agentuser.service.AgentUserService;
import com.scorpio.agentuser.vo.CreateUpdateUserVo;
import com.scorpio.agentuser.vo.UpdatePassword2Vo;
import com.scorpio.common.annotation.DataSource;
import com.scorpio.common.annotation.Log;
import com.scorpio.common.annotation.RepeatSubmit;
import com.scorpio.common.core.controller.BaseController;
import com.scorpio.common.core.domain.AjaxResult;
import com.scorpio.common.core.page.TableDataInfo;
import com.scorpio.common.enums.BusinessType;
import com.scorpio.common.enums.DataSourceType;
import com.scorpio.common.utils.poi.ExcelUtil;
import com.scorpio.common.utils.verification.AddGroup;
import com.scorpio.common.utils.verification.DefaultGroup;
import com.scorpio.common.utils.verification.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代理商用户Controller
 *
 * @author scorpio
 * @date 2020-09-25
 */
@RestController
@RequestMapping("/agent/agent_user")
public class AgentUserController extends BaseController {

    @Autowired
    private AgentUserService service;

    /**
     * 查询代理商用户列表
     */
    @PreAuthorize("@ss.hasPermi('agent:agent_user:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgentUserEntity entity) {
        startPage();
        List<AgentUserEntity> list = service.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出代理商用户列表
     */
    @PreAuthorize("@ss.hasPermi('agent:agent_user:export')")
    @Log(title = "代理商用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AgentUserEntity entity) {
        List<AgentUserEntity> list = service.selectList(entity);
        ExcelUtil<AgentUserEntity> util = new ExcelUtil<>(AgentUserEntity.class);
        return util.exportExcel(list, "agent_user");
    }

    /**
     * 获取代理商用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('agent:agent_user:query')")
    @GetMapping(value = "/{remark}")
    public AjaxResult getInfo(@PathVariable("remark") String remark) {
        return AjaxResult.success(service.selectById(remark));
    }

    /**
     * 新增代理商用户
     */
    @PreAuthorize("@ss.hasPermi('agent:agent_user:add')")
    @Log(title = "代理商用户", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated(value = {DefaultGroup.class, AddGroup.class}) @RequestBody CreateUpdateUserVo vo) {
        return toAjax(service.save(vo));
    }

    /**
     * 修改代理商用户
     */
    @PreAuthorize("@ss.hasPermi('agent:agent_user:edit')")
    @Log(title = "代理商用户", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated(value = {DefaultGroup.class, UpdateGroup.class}) @RequestBody CreateUpdateUserVo vo) {
        return toAjax(service.update(vo));
    }

    /**
     * 删除代理商用户
     */
    @PreAuthorize("@ss.hasPermi('agent:agent_user:remove')")
    @Log(title = "代理商用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{remarks}")
    public AjaxResult remove(@PathVariable String[] remarks) {
        return toAjax(service.deleteByIds(remarks));
    }


    @PreAuthorize("@ss.hasPermi('agent:agent_user:edit')")
    @Log(title = "代理商用户初始化密码", businessType = BusinessType.UPDATE)
    @PutMapping("/resetAgentPwd")
    public AjaxResult resetAgentPwd(@Validated @RequestBody UpdatePassword2Vo vo){
        return toAjax(service.resetAgentPwd(vo.getAgentId(),vo.getPassword()));
    }
}
